// add-address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:[0,0,0],
    area_value:[],
    street:[],
    province:[],
    area:[],
    city:[],
    index:0,
    streetValue:'',
    detail:'',
    isdefault:true,
    accept_name:'',
    phone: '',
    address: '',
    isupdate: false,//是否为编辑,false就是新增
    id:'',
  },
  bindRegionChange(e){
    console.log(e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  detail(e){
    this.setData({
      detail: e.detail.value
    })
  },
  switch2Change(e){
    this.setData({
      isdefault: e.detail.value
    })
  },
  // 提交数据
  submit(){
    let data = {
      access_token: getApp().globalData.access_token,
      accept_name: this.data.accept_name,
      phone: this.data.phone,
      address: this.data.detail,
      province_id: this.data.province[this.data.region[0]].id,
      city_id: this.data.city[this.data.region[1]].id,
      area_id: this.data.area[this.data.region[2]].id,
      street_id: this.data.street[this.data.streetValue].id,
      is_default: this.data.isdefault?'2':'1'
    }
    if (this.data.isupdate){
      data.id = this.data.id
    }
    // 数据校验
    if(data.accept_name.length == 0){
      toast('请输入收货人')
      return
    } else if (!(/^1[34578]\d{9}$/.test(data.phone))){
      toast('请输入正确的手机号')
      return
    } else if (!data.address){
      toast('请输入详细地址')
      return
    }else{
      let self = this
      let baseUrl = getApp().globalData.baseUrl
      wx.showLoading({
        title: '正在' + (this.data.isupdate?'更改':'新增') +'地址...',
      })
      wx.request({
        url: baseUrl + (this.data.isupdate ?'/Address/edit_address':'/Address/add_address'),
        data,
        header:{
          'Content-Type':'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: res => {
          wx.hideLoading()
          if(res.data.code == 200){
            wx.showToast({
              title: '添加成功！',
              complete: function(){
                wx.redirectTo({
                  url: './address',
                })
              }
            })
            
          }else{
            wx.showToast({
              title: '新增失败，请稍后重试',
              image: '../../image/error.png'
            })
          }
        }
      })
    }
    function toast(info){
      wx.showToast({
        title: info,
        image: '../../image/error.png'
      })
    }
    console.log(data)
  },
  inputName(e){
    this.setData({
      accept_name: e.detail.value
    })
    console.log(e.detail.value)
  },
  inputPhone(e){
    this.setData({
      phone: e.detail.value
    })
  },
  bindcolumnchange(e){
    console.log(e.detail)
    let self = this
    
    if (e.detail.column == 0){
      this.getCity(e.detail.value, this.data.province[e.detail.value].id)
    } else if (e.detail.column == 1){
      this.getarea(e.detail.value, this.data.city[e.detail.value].id)
    } else if (e.detail.column == 2){
      this.getStreet(e.detail.value, this.data.area[e.detail.value].id)
    }
  },
  bindPickerChange(e){
    this.setData({
      streetValue: e.detail.value
    })
  },
  // 获取市
  getCity(index,code){
    let self = this
    let province_id = code
    let baseUrl = getApp().globalData.baseUrl
    let access_token = getApp().globalData.access_token
    wx.request({
      url: baseUrl + '/Address/getCity',
      data: { access_token, province_id },
      type: 'get',
      success: data => {
        console.log(data.data)
        if (data.data.code == 200) {
          self.setData({
            city: data.data.data.city,
            area: data.data.data.area,
            street: data.data.data.street
          })
        }
        // wx.hideLoading()
      }
    })
  },
  // 获取区
  getarea(index,code) {
    let self = this
    let city_id = code
    let baseUrl = getApp().globalData.baseUrl
    let access_token = getApp().globalData.access_token
    wx.request({
      url: baseUrl + '/Address/getArea',
      data: { access_token, city_id },
      type: 'get',
      success: data => {
        console.log(data.data)
        if (data.data.code == 200) {
          self.setData({
            area: data.data.data.area,
            street: data.data.data.street
          })
        }
        wx.hideLoading()
      }
    })
  },
  // 获取街道
  getStreet(index,code){
    console.log(666)
    let self = this
    let area_id = code
    let baseUrl = getApp().globalData.baseUrl
    let access_token = getApp().globalData.access_token
    wx.request({
      url: baseUrl + '/Address/getStreet',
      type: 'get',
      data:{
        access_token,
        area_id
      },
      success: data => {
        if(data.data.code == 200){
          self.setData({
            street: data.data.data
          })
        }
      }
    })
  },
  // 请求省份
  getProvince(){
    let baseUrl = getApp().globalData.baseUrl
    let access_token = getApp().globalData.access_token
    let self = this
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: baseUrl + '/Address/add_address',
      type: 'get',
      data: { access_token },
      success: data => {
        console.log(data.data.data.province)
        self.setData({
          province: data.data.data.province,
          city: data.data.data.city,
          area: data.data.data.area,
          street: data.data.data.street
        })
        wx.hideLoading()
      }
    })
  },
  // 编辑模式下获取初始化数据
  getEdit(id){
    let baseUrl = getApp().globalData.baseUrl
    let access_token = getApp().globalData.access_token
    let self = this
    wx.request({
      url: baseUrl +  '/Address/edit_address',
      type: 'get',
      data: { access_token, id},
      success: data => {
        console.log(data.data.data)
        if(data.data.code == 200){
          let region = [],
            streetValue = ''

          data.data.data.province.forEach((value, index) => {
            if (value.id == data.data.data.address.province_id) {
              region.push(index)
              console.log(region,1)
            }
          })
          data.data.data.city.forEach( (value,index) => {
            if (value.id == data.data.data.address.city_id){
              region.push(index)
              console.log(2)
              console.log(region,2)
            } 
          })
          data.data.data.area.forEach((value, index) => {
            if (value.id == data.data.data.address.area_id) {
              region.push(index)
              console.log(region,3)
            }
          })
          data.data.data.street.forEach((value, index) => {
            if (value.id == data.data.data.address.street_id) {
              streetValue = index
            }
          })

          self.setData({
            area: data.data.data.area,
            city: data.data.data.city,
            province: data.data.data.province,
            street: data.data.data.street,
            isdefault: data.data.data.address.is_default,
            accept_name: data.data.data.address.accept_name,
            phone: data.data.data.address.phone,
            detail: data.data.data.address.address,
            region,
            streetValue,
            isupdate: true,
            id: data.data.data.address.id
          })
        }
      }
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    // 如果是编辑模式
    if(options.type == 'edit'){
      this.getEdit(options.id)
    }else{
      this.getProvince()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {

    
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})